import { ref } from 'vue';

import { isEmpty } from '@vben/utils';

import { defineStore } from 'pinia';

import { getNavs } from '#/api/admin/navs';
import { getSlides } from '#/api/admin/slides';
import {
  getArticleSelect,
  getArticleSelectCategorys,
} from '#/api/portal/article';
import { getLinkageData } from '#/api/portal/linkageData';
import { getPage } from '#/api/portal/page';

type TreeData = {
  [key: string]: any[];
};

type DataSourceConfig = {
  fieldNames: { children: string; label: string; value: string };
  onGetData: (params: any) => any[] | Promise<any[]>;
};

export const useDatasourceStore = defineStore('datasource', () => {
  const treeData = ref<TreeData>({
    'admin/slide/index': [],
    'admin/nav/index': [],
    'portal/category/index': [],
    'portal/page/index': [],
    'portal/linkage/index': [],
    'portal/article/index': [],
  });

  const fetchData = async (
    api: string,
    requestFn: any,
    params: any = {},
  ): Promise<any[]> => {
    if (
      isEmpty(treeData.value[api]) ||
      ['portal/linkage/index'].includes(api) ||
      params.filters
    ) {
      if (api === 'portal/linkage/index') {
        treeData.value[api] = params.dataSource.data;
      } else {
        const {
          data: { list },
        } = await requestFn(params.filters);
        treeData.value[api] = list || []; // Ensure that we always set an array, not undefined
      }
    }
    return treeData.value[api] || []; // Return an empty array if it's undefined
  };

  const dataSourceConfigs: Record<string, DataSourceConfig> = {
    'admin/slide/index': {
      fieldNames: { children: 'children', label: 'name', value: 'id' },
      onGetData: () => fetchData('admin/slide/index', getSlides),
    },
    'admin/nav/index': {
      fieldNames: { children: 'children', label: 'name', value: 'id' },
      onGetData: () => fetchData('admin/nav/index', getNavs),
    },
    'portal/category/index': {
      fieldNames: { children: 'children', label: 'name', value: 'id' },
      onGetData: (params: any) =>
        fetchData(
          'portal/category/index',
          getArticleSelectCategorys,
          params.filters,
        ),
    },
    'portal/page/index': {
      fieldNames: { children: 'children', label: 'post_title', value: 'id' },
      onGetData: (params: any) =>
        fetchData('portal/page/index', getPage, params.filters),
    },
    'portal/linkage/index': {
      fieldNames: { children: 'children', label: 'name', value: 'name' },
      onGetData: (params: any) =>
        fetchData('portal/linkage/index', getLinkageData, params),
    },
    'portal/article/index': {
      fieldNames: { children: 'children', label: 'post_title', value: 'id' },
      onGetData: (params: any) =>
        fetchData('portal/article/index', getArticleSelect, params),
    },
  };

  const getdataSource = (api: string, props: any = {}) => {
    const config = dataSourceConfigs[api];

    if (config) {
      const { onGetData, fieldNames } = config;
      return { data: onGetData(props), field_names: fieldNames };
    }

    // If no config is found, return a default structure
    return {
      data: [],
      field_names: {
        children: 'children',
        label: 'name',
        value: 'id',
      },
    };
  };

  const $reset = () => {
    treeData.value = {
      'admin/slide/index': [],
      'admin/nav/index': [],
      'portal/category/index': [],
      'portal/page/index': [],
      'portal/linkage/index': [],
      'portal/article/index': [],
    };
  };

  return {
    $reset,
    getdataSource,
  };
});
